import org.tmatesoft.svn.core.*;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.dav.DAVRepositoryFactory;
import org.tmatesoft.svn.core.io.ISVNEditor;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.io.diff.SVNDeltaGenerator;
import org.tmatesoft.svn.core.wc.SVNWCUtil;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Iterator;

/**
 * Created by PPurohit on 1/7/2015.
 */
public class SVNTest {

    public static void main(String[] args) {
        DAVRepositoryFactory.setup();

        String url = "http://192.168.56.103/repos/testproj/";
        String name = "user1";
        String password = "user1";
        String filePath = "test.txt";

        SVNRepository repository;
        try {
            repository = SVNRepositoryFactory.create(SVNURL.parseURIEncoded(url));
            ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
            repository.setAuthenticationManager(authManager);

            SVNNodeKind nodeKind = repository.checkPath(filePath, -1);

            if (nodeKind == SVNNodeKind.NONE) {
                System.err.println("There is no entry at '" + url + "'.");
                System.exit(1);
            } else if (nodeKind == SVNNodeKind.DIR) {
                System.err.println("The entry at '" + url + "' is a directory while a file was expected.");
                System.exit(1);
            }

            byte[] present = printFile(filePath, repository);

            ISVNEditor editor = repository.getCommitEditor("Commit log", null);
            editor.openRoot(-1);
            editor.openFile("test.txt", -1);

            SVNDeltaGenerator deltaGenerator = new SVNDeltaGenerator();
            byte[] bytes = "new content".getBytes();
            String checksum = deltaGenerator.sendDelta("test.txt", new ByteArrayInputStream(bytes), editor, true);
            editor.applyTextDelta("test.txt", checksum);
            editor.closeFile("test.txt", checksum);
            editor.closeEdit();
            repository.closeSession();
        } catch (SVNException e) {
            e.printStackTrace();
        }


    }

    private static byte[] printFile(String filePath, SVNRepository repository) throws SVNException {
        SVNProperties fileProperties = new SVNProperties();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        repository.getFile(filePath, -1, fileProperties, baos);

        String mimeType = fileProperties.getStringValue(SVNProperty.MIME_TYPE);
        boolean isTextType = SVNProperty.isTextMimeType(mimeType);
        Iterator iterator = fileProperties.asMap().keySet().iterator();
        while (iterator.hasNext()) {
            String propertyName = (String) iterator.next();
            String propertyValue = fileProperties.getStringValue(propertyName);
            System.out.println("File property: " + propertyName + "=" + propertyValue);
        }

        if (isTextType) {
            System.out.println("File contents:");
            System.out.println();
            try {
                baos.writeTo(System.out);
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        } else {
            System.out.println("Not a text file.");
        }
        return baos.toByteArray();
    }
}
